package com.putong35.common.account.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;

import androidx.databinding.BindingAdapter;

import com.putong35.common.account.Account;
import com.putong35.common.account.AccountManager;
import com.putong35.common.account.data.http.AccountApiHelper;
import com.putong35.common.baseactfrag.activity.BaseActivity;
import com.putong35.common.commonui.datetimepicker.DatePickerFragment;
import com.putong35.common.customviews.iconcontent.RichExtraTextBar;
import com.putong35.common.databinding.ActivityMyInformationBinding;
import com.putong35.common.dialog.EditDialogFragment;
import com.putong35.common.dialog.IOSAlertListFragment;
import com.putong35.common.httprequest.OnFailureListener;
import com.putong35.common.httprequest.OnSuccessListener;
import com.putong35.common.utils.EasyToast;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class MyInformationActivity extends BaseActivity {

    public static void start(Context context) {
        Intent starter = new Intent(context, MyInformationActivity.class);
        context.startActivity(starter);
    }

    ActivityMyInformationBinding activityMyInformationBinding;
    Account newAccount;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle("个人资料");
        enableBackButton();
//        getCustomToolbar().addRightButton("完成", v -> {
//            updateAccount(newAccount);
//            finish();
//        });
    }

    @Override
    public View createDataView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        activityMyInformationBinding = ActivityMyInformationBinding.inflate(getLayoutInflater());
        newAccount = AccountManager.getInstance().getAccount().clone();
        activityMyInformationBinding.setAccount(newAccount);
        activityMyInformationBinding.setViewModel(this);
        return activityMyInformationBinding.getRoot();
    }

    void updateAccount(Account newAccount) {
        AccountManager.getInstance().setAccount(newAccount);
    }

    public void showBirthdayChooseDialog() {
        final DatePickerFragment datePickerFragment = new DatePickerFragment();
        datePickerFragment.setCurrentDate(Calendar.getInstance());
        datePickerFragment.setOnDateSelectedListener((year, monthOfYear, dayOfMonth) -> {
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.YEAR, year);
            calendar.set(Calendar.MONTH, monthOfYear-1);
            calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
//            newAccount.setBirthday(calendar.getTimeInMillis());
            datePickerFragment.dismissAllowingStateLoss();
        });
        datePickerFragment.show(getSupportFragmentManager());

    }

    public void clkEditName() {
        final EditDialogFragment alertDialogFragment = new EditDialogFragment();
        alertDialogFragment.setDefaultContent(newAccount.getNickName())
                .setOnCententSavedListener(new EditDialogFragment.OnContentSavedListener() {
                    @Override
                    public void OnContentSaved(CharSequence content) {
//                        EasyToast.showToast(MyInformationActivity.this, "content = " + content);
                        AccountApiHelper.edit(content.toString(), -1, "", new OnSuccessListener<Account>() {
                            @Override
                            public void onSuccess(int code, String msg, Account data, Object extObj) {
                                newAccount = data;
                                AccountManager.getInstance().setAccount(newAccount);
                                activityMyInformationBinding.setAccount(newAccount);

                                EasyToast.showToast(MyInformationActivity.this, "修改成功！");
                            }
                        }, new OnFailureListener() {
                            @Override
                            public void onFailure(int errCode, String msg) {
                                EasyToast.showToast(MyInformationActivity.this, "修改失败：" + msg);
                            }
                        });


                    }
                })
                .setNegativeButton("取消", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        alertDialogFragment.dismissAllowingStateLoss();
                    }
                })
                .setPositiveButton("确定", true, null)
                .setTitle("修改昵称")

        ;

        alertDialogFragment.show(getSupportFragmentManager());
    }


    public void clkEditSex() {
        String[] actionArr = new String[]{"男", "女", "保密"};
        final IOSAlertListFragment diaFrag = new IOSAlertListFragment();
        diaFrag.setActionList(new AdapterView.OnItemClickListener() {
                                  @Override
                                  public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                      Log.e("vivi", "list action button clicked  position == " + position);
                                      // 男1  女2  保密0
                                      int sexValue = position == 0? 1: (position == 1)? 2: 0;
                                      AccountApiHelper.edit("", sexValue, "", new OnSuccessListener<Account>() {
                                          @Override
                                          public void onSuccess(int code, String msg, Account data, Object extObj) {
                                              newAccount = data;
                                              AccountManager.getInstance().setAccount(newAccount);
                                              activityMyInformationBinding.setAccount(newAccount);

                                              EasyToast.showToast(MyInformationActivity.this, "修改成功！");
                                          }
                                      }, new OnFailureListener() {
                                          @Override
                                          public void onFailure(int errCode, String msg) {
                                              EasyToast.showToast(MyInformationActivity.this, "修改失败：" + msg);
                                          }
                                      });
                                      diaFrag.dismiss();
                                  }
                              },
                actionArr)
                .show(getSupportFragmentManager());
    }

    @BindingAdapter({"birthdayFormat"})
    public static void imageLoader(RichExtraTextBar textBar, long timeInMills) {
        String formatStr = "yyyy-MM-dd";
        SimpleDateFormat format = new SimpleDateFormat(formatStr, Locale.CHINA);
        textBar.setExtraText(format.format(new Date(timeInMills)));
    }

}
